package sk.vrto.web.views;

import com.vaadin.Application;
import com.vaadin.event.LayoutEvents;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.*;
import sk.vrto.web.ApplicationInjector;
import sk.vrto.web.presenters.EmailApplicationPresenter;
import sk.vrto.web.presenters.ContactsListPresenter;
import sk.vrto.web.presenters.NewContactPresenter;

public class EmailApplicationView extends Application {

    // Application is Entry point, therefore it muse handle delegation of presenters
    protected ApplicationInjector applicationInjector;
    protected EmailApplicationPresenter presenter;

    private Window newContactWindow;
    private Window newEmailWindow;
    private VerticalLayout contactList;
    protected VerticalLayout leftWrapper;

    @Override
    public void init() {
        presenter = ApplicationInjector.getEmailApplicationPresenter();
        presenter.setView(this);

        setMainWindow(new Window("miEmail application"));
        setTheme("email");
        HorizontalLayout appLayout = new HorizontalLayout();
        appLayout.setSizeFull();

        appLayout.addComponent(buildLeftWrapper());
        Component rightWrapper = buildRightWrapper();
        appLayout.addComponent(rightWrapper);
        appLayout.setExpandRatio(rightWrapper, 1.0f);
//
        getMainWindow().setContent(appLayout);
        createNewMailWindow();
    }

    public Window getNewContactWindow() {
        return newContactWindow;
    }

    public Window getNewEmailWindow() {
        return newEmailWindow;
    }

    private void createNewMailWindow() {
        final NewEmailView emailView = new NewEmailView(ApplicationInjector.getNewEmailPresenter());
        newEmailWindow = new Window("New message", emailView);
        newEmailWindow.setModal(true);
        newEmailWindow.setWidth("80%");
        newEmailWindow.setHeight("70%");
        newEmailWindow.setVisible(false);
        newEmailWindow.addListener(new Window.CloseListener() {
            @Override
            public void windowClose(Window.CloseEvent e) {
                ContactViewPopup triggeredFrom = emailView.getTriggeredFrom();
                if (null != triggeredFrom) {
                    triggeredFrom.setVisible(true);
                    emailView.setTriggeredFrom(null);
                }
                emailView.getContactsBox().setValue(null);
                emailView.getTitle().setValue("");
                emailView.getMailArea().setValue("");
            }
        });
    }

    private Component buildRightWrapper() {
        VerticalLayout wrapper = new VerticalLayout();
        wrapper.setStyleName("marged");
        wrapper.setSpacing(true);
        wrapper.addComponent(buildLogoutMenu());

        Label recentMail = new Label("Recent mail");
        recentMail.setStyleName("recent-mail");
        wrapper.addComponent(recentMail);

        wrapper.addComponent(buildButtonsMenu());
        wrapper.addComponent(new EmailsView(ApplicationInjector.getEmailsPresenter(), 6));
        return wrapper;
    }

    private Component buildLogoutMenu() {
        HorizontalLayout menu = new HorizontalLayout();
        menu.setWidth("100%");

        Label labelLogout = new Label("Logout Demo User");
        labelLogout.setStyleName("logout-link");
        labelLogout.setWidth("120px");
        menu.addComponent(labelLogout);
        menu.addListener(new LayoutEvents.LayoutClickListener() {
            @Override
            public void layoutClick(LayoutEvents.LayoutClickEvent event) {
                presenter.closeApplication();
            }
        });
        menu.setComponentAlignment(labelLogout, Alignment.MIDDLE_RIGHT);

        return menu;
    }

    private Component buildButtonsMenu() {
        HorizontalLayout buttonsMenu = new HorizontalLayout();
        buttonsMenu.setSpacing(true);
        buttonsMenu.addComponent(createNewMailButton());
        buttonsMenu.addComponent(createRefreshMailButton());

        Component settingsButton = createSettingsButton();
        buttonsMenu.addComponent(settingsButton);
        buttonsMenu.setComponentAlignment(settingsButton, Alignment.MIDDLE_RIGHT);

        return buttonsMenu;
    }

    private Component createSettingsButton() {
        Button settingsButton = new Button();
        settingsButton.setIcon(new ThemeResource("../email/icons/settings.png"));
        settingsButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                getMainWindow().showNotification("No settings in this demo version");
            }
        });
        return settingsButton;
    }

    private Component createRefreshMailButton() {
        Button refreshMailButton = new Button();
        refreshMailButton.setIcon(new ThemeResource("../email/icons/refresh.png"));
        refreshMailButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                getMainWindow().showNotification("No email account bound in demo");
            }
        });
        return refreshMailButton;
    }

    private Component createNewMailButton() {
        Button newMailButton = new Button("New message");
        newMailButton.setWidth("100px");
        newMailButton.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {

                // not yet showing?
                if (null == newEmailWindow.getParent()) {
                    getMainWindow().addWindow(newEmailWindow);
                }

                newEmailWindow.setVisible(true);
                newEmailWindow.center();
            }
        });
        return newMailButton;
    }

    private Component buildLeftWrapper() {
        VerticalLayout cssWrapper = new VerticalLayout();
        cssWrapper.setStyleName("contacts-panel");
        cssWrapper.setHeight("100%");
        cssWrapper.setWidth("170px");

        leftWrapper = new VerticalLayout();
        leftWrapper.setStyleName("contacts-panel");
        leftWrapper.setWidth("170px");

        Embedded pic = new Embedded("", new ThemeResource("../email/icons/contacts-top-mail.png"));
        pic.setStyleName("contacts-logo");

        leftWrapper.addComponent(pic);
        leftWrapper.addComponent(buildContactsList());
        leftWrapper.addComponent(buildAddContactButton());

        cssWrapper.addComponent(leftWrapper);
        return cssWrapper;
    }

    private Component buildAddContactButton() {
        Button button = new Button("New contact");
        button.setWidth("100px");
        button.setStyleName("add-contact-btn");
        button.addListener(new Button.ClickListener() {
            @Override
            public void buttonClick(Button.ClickEvent event) {
                if (null == newContactWindow) {
                    NewContactPresenter presenter = ApplicationInjector.getNewContactPresenter();
                    newContactWindow = new Window("Add New Contact", new NewContactView(presenter));
                    newContactWindow.setModal(true);
                    newContactWindow.setHeight("200px");
                    newContactWindow.setWidth("220px");
                }

                // not yet showing?
                if (null == newContactWindow.getParent()) {
                    getMainWindow().addWindow(newContactWindow);
                }

                newContactWindow.center();
            }
        });
        return button;
    }

    private ContactsListView buildContactsList() {
        ContactsListPresenter contactsListPresenter =
                ApplicationInjector.getContactsListPresenter();
        ContactsListView view = new ContactsListView(contactsListPresenter,
                ApplicationInjector.getContactPresenter());

        contactsListPresenter.loadAllContacts();
        return view;
    }

}
